//
//  JHSearchWordCell.m
//  JuniRengLish_iOS
//
//  Created by Maiyou on 2019/5/27.
//  Copyright © 2019 revogi. All rights reserved.
//

#import "JHSearchWordCell.h"

@implementation JHSearchWordCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)setDataDic:(NSDictionary *)dataDic
{
    _dataDic = dataDic;
    
    self.translateBefore.text = dataDic[@"word"];
    
    if ([dataDic[@"status"] integerValue] == 0)
    {
        NSString * explain = @"";
        NSArray * array = dataDic[@"word_mean"];
        for (int i = 0; i < [array count]; i ++)
        {
            [explain isEqualToString:@""] ? explain = array[i] : (explain = [NSString stringWithFormat:@"%@\n%@", explain, array[i]]);
        }
        self.translateAfter.text  = explain;
        
        [self.btnImage1 setImage:[UIImage imageNamed:@"right_gray_row"] forState:0];
    }
    else if ([dataDic[@"status"] integerValue] == 1)
    {
        self.translateAfter.text  = dataDic[@"out"];
        
        [self.btnImage1 setImage:[UIImage imageNamed:@"播放"] forState:0];
    }
    
    CGFloat nowTime = [[DJTool getNowTimeTimestamp] doubleValue];
    CGFloat pastTime = [dataDic[@"timestamp"] doubleValue];
    CGFloat time = nowTime - pastTime;
    if (time == 0)
    {
        self.showTime.text = @"1秒前";
    }
    else if (time < 60)
    {
        self.showTime.text = [NSString stringWithFormat:@"%.f秒前", time];
    }
    else if (time < 3600)
    {
        self.showTime.text = [NSString stringWithFormat:@"%.f分钟前", time / 60];
    }
    else if (time < 3600 * 24)
    {
        self.showTime.text = [NSString stringWithFormat:@"%.f小时前", time / 3600];
    }
    else if (time > 3600 * 24)
    {
        self.showTime.text = [NSString stringWithFormat:@"%.f天前", time / 3600 * 24];
    }
}

- (IBAction)buttonClick:(id)sender
{
    UIButton * button = sender;
    if ([_dataDic[@"status"] integerValue] == 0)
    {
        button.tag == 10 ? : [self playWord:self.translateBefore.text Lan:@"uk"];
    }
    else if ([_dataDic[@"status"] integerValue] == 1)
    {
        button.tag == 10 ? [self playWord:self.translateBefore.text Lan:@"zh"] : [self playWord:self.translateAfter.text Lan:@"en"];
    }
}

- (FSAudioStream *)audioStream
{
    if (!_audioStream)
    {
        //创建FSAudioStream对象
        _audioStream = [[FSAudioStream alloc]init];
        _audioStream.onFailure = ^(FSAudioStreamError error,NSString *description){
            NSLog(@"播放过程中发生错误，错误信息：%@",description);
        };
        _audioStream.onCompletion = ^(){
            NSLog(@"播放完成!");
        };
        _audioStream.onStateChange = ^(FSAudioStreamState state) {
            
        };
        [_audioStream setVolume:1];//设置声音
    }
    return _audioStream;
}

- (void)playWord:(NSString *)word Lan:(NSString *)lan
{
    [DJToast showBottomToast:@"正在发音..."];
    
    word = [word stringByAddingPercentEncodingWithAllowedCharacters:  [NSCharacterSet URLUserAllowedCharacterSet]];
    NSString * urlStr = [NSString stringWithFormat:@"https://fanyi.baidu.com/gettts?lan=%@&text=%@&spd=2&source=web", lan, word];
    self.audioStream.url = [NSURL URLWithString:urlStr];
    [self.audioStream play];
}

@end
